package com.hoshiibuy.organization.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.organization.entity.StoreOrganization;
import com.hoshiibuy.organization.entity.param.StoreOrganizationQueryParam;
import com.hoshiibuy.organization.entity.param.StoreOrganizationUpdateParam;
import com.hoshiibuy.organization.entity.param.StoreOrganizationAddParam;
import com.hoshiibuy.organization.service.StoreOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.hoshiibuy.common.utils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 店铺（伙伴/供应商) 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-04-28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/store-organization")
@Api(value = "供应商信息", tags = {"店铺（伙伴/供应商)管理"})
public class StoreOrganizationController {
    @Autowired
    private StoreOrganizationService storeOrganizationService;

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 分页查询店铺（伙伴/供应商)
    */
    @ApiOperation(value = "分页查询店铺（伙伴/供应商)")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid StoreOrganizationQueryParam param) {
        try {
            log.info("分页查询店铺（伙伴/供应商)");
            QueryWrapper<StoreOrganization> queryWrapper = new QueryWrapper<StoreOrganization>();
            queryWrapper.eq(param.getPlatid() != null, "platid", param.getPlatid());
            queryWrapper.eq(param.getStoreId() != null, "store_id", param.getStoreId());
            queryWrapper.eq(param.getType() != null, "type", param.getType());
            queryWrapper.eq(param.getParentid() != null, "parentid", param.getParentid());
            queryWrapper.eq(StringUtils.isNotBlank(param.getName()), "name", param.getName());
            queryWrapper.eq(StringUtils.isNotBlank(param.getPassword()), "password", param.getPassword());
            queryWrapper.eq(StringUtils.isNotBlank(param.getCharge()), "charge", param.getCharge());
            queryWrapper.eq(StringUtils.isNotBlank(param.getIdCard()), "id_card", param.getIdCard());
            queryWrapper.eq(StringUtils.isNotBlank(param.getBankNumber()), "bank_number", param.getBankNumber());
            queryWrapper.eq(StringUtils.isNotBlank(param.getBankName()), "bank_name", param.getBankName());
            queryWrapper.eq(StringUtils.isNotBlank(param.getBankAddress()), "bank_address", param.getBankAddress());
            queryWrapper.eq(StringUtils.isNotBlank(param.getTaxation()), "taxation", param.getTaxation());
            queryWrapper.eq(StringUtils.isNotBlank(param.getPiaohao()), "piaohao", param.getPiaohao());
            queryWrapper.eq(StringUtils.isNotBlank(param.getTelphone()), "telphone", param.getTelphone());
            queryWrapper.eq(StringUtils.isNotBlank(param.getMobile()), "mobile", param.getMobile());
            queryWrapper.eq(StringUtils.isNotBlank(param.getFax()), "fax", param.getFax());
            queryWrapper.eq(StringUtils.isNotBlank(param.getHome()), "home", param.getHome());
            queryWrapper.eq(StringUtils.isNotBlank(param.getEmail()), "email", param.getEmail());
            queryWrapper.eq(StringUtils.isNotBlank(param.getAddress()), "address", param.getAddress());
            queryWrapper.eq(StringUtils.isNotBlank(param.getGoodsAddress()), "goods_address", param.getGoodsAddress());
            queryWrapper.eq(StringUtils.isNotBlank(param.getWhouseAddress()), "whouse_address", param.getWhouseAddress());
            queryWrapper.eq(param.getPaymentId() != null, "payment_id", param.getPaymentId());
            IPage<StoreOrganization> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            queryWrapper.select(BeanUtils.getSqlSelect(StoreOrganization.class));
            return Response.data(storeOrganizationService.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询店铺（伙伴/供应商)异常", e);
            return Response.failed("分页查询店铺（伙伴/供应商)异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 根据id查询店铺（伙伴/供应商)
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询店铺（伙伴/供应商)")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(storeOrganizationService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增店铺（伙伴/供应商)")
    public Response save(@RequestBody @Valid StoreOrganizationAddParam param) {
        try {
            StoreOrganization data = BeanUtils.covertType(param, StoreOrganization.class);
            return Response.data(storeOrganizationService.save(data));
        } catch (Exception e) {
            log.error("新增店铺（伙伴/供应商)异常", e);
            return Response.failed();
        }
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 修改店铺（伙伴/供应商)
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改店铺（伙伴/供应商)")
    public Response update(@RequestBody @Valid StoreOrganizationUpdateParam param) {
        try {
            StoreOrganization data = BeanUtils.covertType(param, StoreOrganization.class);
            return Response.data(storeOrganizationService.updateById(data));
        } catch (Exception e) {
            log.error("修改店铺（伙伴/供应商)异常", e);
            return Response.failed();
        }
    }
}
